<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/common.js">
    </script>
    <style>
        #all {
            width: 400px;
            text-align: center;
            margin: 0 auto;

        }

        #tab {
            width: 300px;
            height: 150px;
            border-collapse: collapse;
        }

        table thead {
            background-color: #0099cd;
            color: #fff;
        }
    </style>
    <script>
        window.onload = function () {
            var tab = my$('tab');
            var th = my$('th');
            var tb = my$('tb');
            var th_input = th.firstElementChild.firstElementChild.firstElementChild;
            th_input.onclick = function () {
                for (var i = 0; i < tb.children.length; i++) {
                    tb.children[i].firstElementChild.firstElementChild.checked = this['checked'];
                }
            }
            for (var i = 0; i < tb.children.length; i++) {
                tb.children[i].firstElementChild.firstElementChild.onclick = function() {
                    for (var i = 0; i < tb.children.length; i++) {
                        if(!tb.children[i].firstElementChild.firstElementChild.checked) {
                            th_input.checked = '';
                            return;
                        }
                    }
                    th_input.checked = 'checked';
                }
            }
        }
    </script>
</head>

<body>
    <div id="all">
        <table id="tab" border="1" cellspacing="0">
            <thead id="th">
                <tr>
                    <th><input type="checkbox"></th>
                    <th>菜名</th>
                    <th>饭店</th>
                </tr>

            </thead>
            <tbody id="tb">
                <tr>
                    <td><input type="checkbox"></td>
                    <td>红烧肉</td>
                    <td>田老师</td>
                </tr>
                <tr>
                    <td><input type="checkbox"></td>
                    <td>水煮肉</td>
                    <td>田老师</td>
                </tr>
                <tr>
                    <td><input type="checkbox"></td>
                    <td>爆炒同桌</td>
                    <td>田老师</td>
                </tr>
            </tbody>
        </table>
    </div>

</body>

</html>